package com.harmontronics.app.pedestal.update;

import androidx.annotation.NonNull;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.FileCallback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.xuexiang.xupdate.proxy.IUpdateHttpService;

import java.io.File;
import java.util.Map;
import java.util.TreeMap;

public class OkGoHttpService implements IUpdateHttpService {

    @Override
    public void asyncGet(@NonNull String url, @NonNull Map<String, Object> params, final @NonNull Callback callBack) {
        OkGo.<String>get(url).tag(url).params(transform(params)).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                callBack.onSuccess(response.body());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callBack.onError(response.getException());
            }
        });
    }

    @Override
    public void asyncPost(@NonNull String url, @NonNull Map<String, Object> params, final @NonNull Callback callBack) {
        //这里默认post的是Form格式
        OkGo.<String>post(url).tag(url).params(transform(params)).isMultipart(true).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                callBack.onSuccess(response.body());
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                callBack.onError(response.getException());
            }
        });
    }

    @Override
    public void download(@NonNull String url, @NonNull String path, @NonNull String fileName, final @NonNull DownloadCallback callback) {
        OkGo.<File>get(url).tag(url).execute(new FileCallback() {
            @Override
            public void onSuccess(Response<File> response) {
                callback.onSuccess(response.body());
            }

            @Override
            public void onStart(com.lzy.okgo.request.base.Request<File, ? extends com.lzy.okgo.request.base.Request> request) {
                super.onStart(request);
                callback.onStart();
            }

            @Override
            public void onError(Response<File> response) {
                super.onError(response);
                callback.onError(response.getException());
            }

            @Override
            public void downloadProgress(Progress progress) {
                super.downloadProgress(progress);
                callback.onProgress(progress.currentSize, progress.totalSize);
            }
        });
    }

    @Override
    public void cancelDownload(@NonNull String url) {
        OkGo.getInstance().cancelTag(url);
    }

    private Map<String, String> transform(Map<String, Object> params) {
        Map<String, String> map = new TreeMap<>();
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            map.put(entry.getKey(), entry.getValue().toString());
        }
        return map;
    }


}